/**
 * 玩家实体类
 * 定义AI侦探应用中的玩家数据结构
 */
export class Player {
  // 玩家ID
  id: string;
  
  // 玩家名称
  name: string;
  
  // 玩家头像
  avatar: string;
  
  // 等级
  level: number;
  
  // 经验值
  experience: number;
  
  // 积分
  points: number;
  
  // 已解决案件数
  solvedCases: number;
  
  // 正在调查的案件ID
  currentCaseId?: string;
  
  // 角色类型
  roleType: 'amateur' | 'detective' | 'master' | 'genius';
  
  // 已解决的案件ID列表
  casesSolved: string[];
  
  // 技能点分配
  skills: {
    observation: number;
    reasoning: number;
    deduction: number;
    intuition: number;
  };
  
  // 收集的线索ID列表
  collectedClueIds: string[];
  
  // 发现的证据ID列表
  discoveredEvidenceIds: string[];
  
  // 创建时间
  createdAt: Date;
  
  // 最后在线时间
  lastOnlineAt: Date;
  
  /**
   * 构造函数
   * @param playerData 玩家数据
   */
  constructor(playerData: {
    id?: string;
    name: string;
    avatar: string;
    level?: number;
    experience?: number;
    points?: number;
    solvedCases?: number;
    currentCaseId?: string;
    roleType?: 'amateur' | 'detective' | 'master' | 'genius';
    skills?: {
      observation: number;
      reasoning: number;
      deduction: number;
      intuition: number;
    };
    collectedClueIds?: string[];
    discoveredEvidenceIds?: string[];
    casesSolved?: string[];
    createdAt?: Date;
    lastOnlineAt?: Date;
  }) {
    this.id = playerData.id || this.generateId();
    this.name = playerData.name;
    this.avatar = playerData.avatar;
    this.level = playerData.level || 1;
    this.experience = playerData.experience || 0;
    this.points = playerData.points || 0;
    this.solvedCases = playerData.solvedCases ?? (playerData.casesSolved?.length ?? 0);
    this.currentCaseId = playerData.currentCaseId;
    this.roleType = playerData.roleType || 'amateur';
    this.casesSolved = playerData.casesSolved || [];
    this.skills = playerData.skills || {
      observation: 1,
      reasoning: 1,
      deduction: 1,
      intuition: 1
    };
    this.collectedClueIds = playerData.collectedClueIds || [];
    this.discoveredEvidenceIds = playerData.discoveredEvidenceIds || [];
    this.createdAt = playerData.createdAt || new Date();
    this.lastOnlineAt = playerData.lastOnlineAt || new Date();
  }
  
  /**
   * 生成唯一ID
   * @returns 唯一ID字符串
   */
  private generateId(): string {
    return `player_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`;
  }
  
  /**
   * 升级玩家等级
   */
  levelUp(): void {
    this.level++;
    this.experience = 0;
    // 每次升级获得技能点
    this.skills.observation++;
    this.skills.reasoning++;
    this.skills.deduction++;
    this.skills.intuition++;
  }
  
  /**
   * 添加经验值
   * @param amount 要添加的经验值
   * @returns 是否升级
   */
  addExperience(amount: number): boolean {
    this.experience += amount;
    const requiredExp = this.level * 100;
    const didLevelUp = this.experience >= requiredExp;
    
    if (didLevelUp) {
      this.levelUp();
    }
    
    return didLevelUp;
  }
  
  /**
   * 添加积分
   * @param amount 要添加的积分
   */
  addPoints(amount: number): void {
    this.points += amount;
  }
  
  /**
   * 收集线索
   * @param clueId 线索ID
   * @returns 是否成功收集（如果已收集则返回false）
   */
  collectClue(clueId: string): boolean {
    if (this.collectedClueIds.includes(clueId)) {
      return false;
    }
    this.collectedClueIds.push(clueId);
    return true;
  }
  
  /**
   * 发现证据
   * @param evidenceId 证据ID
   * @returns 是否成功发现（如果已发现则返回false）
   */
  discoverEvidence(evidenceId: string): boolean {
    if (this.discoveredEvidenceIds.includes(evidenceId)) {
      return false;
    }
    this.discoveredEvidenceIds.push(evidenceId);
    return true;
  }
  
  /**
   * 更新最后在线时间
   */
  updateLastOnline(): void {
    this.lastOnlineAt = new Date();
  }
  
  /**
   * 转换为JSON对象
   * @returns JSON格式的玩家数据
   */
  toJSON() {
    return {
      id: this.id,
      name: this.name,
      avatar: this.avatar,
      level: this.level,
      experience: this.experience,
      points: this.points,
      solvedCases: this.solvedCases,
      currentCaseId: this.currentCaseId ?? null,
      roleType: this.roleType,
      skills: this.skills,
      collectedClueIds: this.collectedClueIds,
      discoveredEvidenceIds: this.discoveredEvidenceIds,
      casesSolved: this.casesSolved,
      createdAt: this.createdAt.toISOString(),
      lastOnlineAt: this.lastOnlineAt.toISOString()
    };
  }
  
  /**
   * 从JSON对象创建Player实例
   * @param json JSON格式的玩家数据
   * @returns Player实例
   */
  static fromJSON(json: any): Player {
    return new Player({
      id: json.id,
      name: json.name,
      avatar: json.avatar,
      level: json.level,
      experience: json.experience,
      points: json.points,
      solvedCases: json.solvedCases,
      currentCaseId: json.currentCaseId,
      roleType: json.roleType,
      skills: json.skills,
      collectedClueIds: json.collectedClueIds,
      discoveredEvidenceIds: json.discoveredEvidenceIds,
      casesSolved: json.casesSolved,
      createdAt: json.createdAt ? new Date(json.createdAt) : new Date(),
      lastOnlineAt: json.lastOnlineAt ? new Date(json.lastOnlineAt) : new Date()
    });
  }
}